import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Settings implements ActionListener
{
	File inputFile = new File(System.getProperty("user.home", new StringBuilder(String.valueOf(File.separatorChar)).append("home").append(File.separatorChar).append("user").toString()) + File.separatorChar + "AppData" + File.separatorChar + "Roaming" + File.separatorChar + ".spades" + File.separatorChar + "settings.dat");
	File directory = new File(System.getProperty("user.home", new StringBuilder(String.valueOf(File.separatorChar)).append("home").append(File.separatorChar).append("user").toString()) + File.separatorChar + "AppData" + File.separatorChar + "Roaming" + File.separatorChar + ".spades");
	Scanner scn;
	JFrame settings = new JFrame("Settings");
	final Font labels = new Font("Times New Roman", 1, 14);
	boolean display;
	State s = null;
	JRadioButton blue = null;
  	JRadioButton grey = null;
  	JRadioButton green = null;
  	JRadioButton yes = null;
  	JRadioButton no = null;
  	JTextField name;
  	JComboBox score;
  	JComboBox leader;
  	JButton save;
  	JButton cancel;
  	String[] setts = new String[5];
  	String[] options = { "200", "250", "500", "750", "1000" };
  	String[] lea = { "0", "1", "2", "3" };
  	boolean bool = false;
  	
  	public Settings(State state) {
  		checkDirs();
  		init();
  		s = state;
  	}
  	
  	public void init() 
  	{
  		getSettingsFromFile();
  		settings.setSize(855, 700);
  		settings.setResizable(false);
  		settings.setLocation(462, 190);
  		settings.setLayout(new FlowLayout());
  		settings.setDefaultCloseOperation(2);
  		settings.setAlwaysOnTop(true);
  		JLabel background = new JLabel(new ImageIcon(getClass().getResource("/resources/settings_background.jpg")));
  		background.setBounds(-5, -5, settings.getWidth(), settings.getHeight());
  		settings.getLayeredPane().add(background, new Integer(-2147483648));
  		background.setOpaque(false);
  		settings.add(background);
  		settings.setForeground(Color.white);
  		
  		JLabel temp = new JLabel("Players Name:");
  		temp.setLocation(50, 50);
  		temp.setSize(100, 20);
  		settings.getLayeredPane().add(temp, 1);
  		
  		name = new JTextField("");
  		name.setText(setts[0]);
  		name.setSelectionEnd(14);
  		name.setLocation(140, 50);
  		name.setSize(100, 20);
  		settings.getLayeredPane().add(name, 1);
  		
  		JLabel backColor = new JLabel("Background Color:");
  		backColor.setLocation(26, 75);
  		backColor.setSize(140, 20);
  		settings.getLayeredPane().add(backColor, 1);
  		
  		green = new JRadioButton("Green");
  		green.setLocation(140, 75);
  		green.setSize(70, 20);
    	green.setOpaque(false);
    	green.setForeground(Color.green);
    	green.addActionListener(this);
    	green.setActionCommand("green");
    	settings.getLayeredPane().add(green, 1);

    	blue = new JRadioButton("Blue");
    	blue.setLocation(140, 100);
    	blue.setSize(70, 20);
    	blue.setOpaque(false);
    	blue.setForeground(Color.blue);
    	blue.addActionListener(this);
    	blue.setActionCommand("blue");
	    settings.getLayeredPane().add(blue, 1);
	
	    grey = new JRadioButton("Light Grey");
	    grey.setLocation(140, 125);
	    grey.setSize(100, 20);
	    grey.setOpaque(false);
	    grey.setForeground(Color.lightGray);
	    grey.addActionListener(this);
	    grey.setActionCommand("lightGrey");
	    settings.getLayeredPane().add(grey, 1);

	    if (setts[1].equals("green"))
	      green.setSelected(true);
	    else if (setts[1].equals("blue"))
	      blue.setSelected(true);
	    else if (setts[1].equals("grey")) {
	      grey.setSelected(true);
	    }
	    JLabel s = new JLabel("Play to:");
	    s.setLocation(50, 150);
	    s.setSize(100, 20);
	    settings.getLayeredPane().add(s, 1);
	
	    score = new JComboBox(options);
	    for (int ctr = 0; ctr < options.length; ctr++)
	      if (options[ctr].equals(setts[2]))
	        score.setSelectedIndex(ctr);
	    score.setLocation(140, 150);
	    score.setSize(100, 20);
	    settings.getLayeredPane().add(score, 1);
	
	    JLabel l = new JLabel("First Dealer:");
	    l.setLocation(50, 175);
	    l.setSize(100, 20);
	    settings.getLayeredPane().add(l, 1);
	
	    String[] leads = { "North", "South", "East", "West" };
	    leader = new JComboBox(leads);
	    for (int ctr = 0; ctr < leads.length; ctr++)
	      if (lea[ctr].equals(setts[3]))
	        leader.setSelectedIndex(ctr);
	    leader.setSelectedIndex(1);
	    leader.setLocation(140, 175);
	    leader.setSize(100, 20);
	    settings.getLayeredPane().add(leader, 1);
	
	    JLabel b = new JLabel("Break Spades:");
	    b.setLocation(40, 200);
	    b.setSize(100, 20);
	    settings.getLayeredPane().add(b, 1);
	
	    yes = new JRadioButton("Yes");
	    yes.setLocation(140, 200);
	    yes.setSize(100, 20);
	    yes.setOpaque(false);
	    yes.addActionListener(this);
	    yes.setActionCommand("yes");
	    settings.getLayeredPane().add(yes, 1);
	
	    no = new JRadioButton("No");
	    no.setLocation(140, 225);
	    no.setSize(100, 20);
	    no.setOpaque(false);
	    no.addActionListener(this);
	    no.setActionCommand("no");
	    settings.getLayeredPane().add(no, 1);
	
	    if (setts[4].equals("true"))
	      yes.setSelected(true);
	    else {
	      no.setSelected(true);
	    }
	    save = new JButton("Save");
	    save.setLocation(50, 250);
	    save.setSize(80, 20);
	    save.setActionCommand("save");
	    save.addActionListener(this);
	    settings.getLayeredPane().add(save, 1);
	
	    cancel = new JButton("Cancel");
	    cancel.setLocation(140, 250);
	    cancel.setSize(80, 20);
	    cancel.setActionCommand("cancel");
	    cancel.addActionListener(this);
	    settings.getLayeredPane().add(cancel, 1);
	    getSettingsFromFile();
	    settings.pack();
	  }
	
	  public JFrame getJFrame() {
	    return settings;
	  }
	
	  public void checkDirs()
	  {
	    if (!directory.exists())
	    {
	      directory.mkdir();
	    }
	    if (!inputFile.exists())
	    {
	      PrintWriter p = null;
	      try {
	        inputFile.createNewFile();
	        p = new PrintWriter(inputFile);
	      } catch (FileNotFoundException e) {
	        e.printStackTrace();
	      } catch (IOException e) {
	        e.printStackTrace();
	      }
	      p.println("Player-Name:Andrew Markley");
	      p.println("Background-Color:green");
	      p.println("Play-To:500");
	      p.println("First-Dealer:1");
	      p.println("Break-Spades:true");
	      p.close();
	    }
	  }
	
	  public void getSettingsFromFile()
	  {
	    try
	    {
	      Scanner r = new Scanner(inputFile);
	      for (int ctr = 0; ctr < 5; ctr++)
	      {
	        String temp = r.nextLine();
	        String ans = temp.split(":")[1];
	
	        setts[ctr] = ans;
	      }
	      r.close();
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	  }
	
	  public void writeToFile() {
	    try {
	      PrintWriter p = new PrintWriter(inputFile);
	      p.println("Player-Name:" + setts[0]);
	      p.println("Background-Color:" + setts[1]);
	      p.println("Play-To:" + setts[2]);
	      p.println("First-Dealer:" + setts[3]);
	      p.println("Break-Spades:" + setts[4]);
	      p.close();
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	  }
	
	  public void actionPerformed(ActionEvent e) {
	    if (e.getActionCommand().equals("blue"))
	    {
	      grey.setSelected(false);
	      green.setSelected(false);
	    }
	    if (e.getActionCommand().equals("lightGrey"))
	    {
	      blue.setSelected(false);
	      green.setSelected(false);
	    }
	    if (e.getActionCommand().equals("green"))
	    {
	      blue.setSelected(false);
	      grey.setSelected(false);
	    }
	    if (e.getActionCommand().equals("yes"))
	    {
	      no.setSelected(false);
	    }
	    if (e.getActionCommand().equals("no"))
	    {
	      yes.setSelected(false);
	    }
	    if (e.getActionCommand().equals("save"))
	    {
		      setts[0] = name.getText().trim();
		      if (green.isSelected())
		        setts[1] = green.getActionCommand();
		      else if (blue.isSelected())
		        setts[1] = blue.getActionCommand();
		      else if (grey.isSelected())
		        setts[1] = grey.getActionCommand();
		      setts[2] = options[score.getSelectedIndex()];
		      setts[3] = lea[leader.getSelectedIndex()];
		      if (yes.isSelected())
		        setts[4] = "true";
		      else
		        setts[4] = "false";
		      writeToFile();
		    	if(bool)
		    		s.setState(0);
		    	else
		    		s.setState(s.getPrevState());
		      settings.setVisible(false);
		      settings.dispose();
	    }
	    if (e.getActionCommand().equals("cancel"))
	    {
	    	if(bool)
	    		s.setState(0);
	    	else
	    		s.setState(s.getPrevState());
	    	settings.setVisible(false);
	    	settings.dispose();
	    }
	  }
	  public void setShowing(boolean b)
	  {
		  bool = b;
	  }
	  public String getPlayerName()
	  {
	    return setts[0];
	  }
	
	  public Color getBackGroundColor() {
	    if (setts[1].equals("green"))
	      return Color.green;
	    if (setts[1].equals("blue")) {
	      return Color.blue;
	    }
	    return Color.lightGray;
	  }
	
	  public int getScoreLimit() 
	  {
	    return Integer.parseInt(setts[2]);
	  }
	
	  public int getLeader() 
	  {
	    return Integer.parseInt(setts[3]);
	  }
	
	  public boolean getBreakSpadesFirst()
	  {
	    return setts[4].equals("true");
	  }
	}